package com.yanqu.road.dataHelper.repair.logic;

import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.player.UserDecoration;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/9/22
 */
public class RepairDecoration151 extends BaseRepair {

    public RepairDecoration151(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {
        List<UserDecoration> userDecorationList = getUserDecorationList();

        Map<Integer, Map<Long, List<UserDecoration>>> map = new ConcurrentHashMap<>();
        map.put(106640, new ConcurrentHashMap<>());
        map.put(106641, new ConcurrentHashMap<>());
        map.put(106642, new ConcurrentHashMap<>());
        map.put(106643, new ConcurrentHashMap<>());
        map.put(106644, new ConcurrentHashMap<>());


        long nowTime = System.currentTimeMillis() / 1000;
        for (UserDecoration userDecoration : userDecorationList) {
            // 合并分类

            if (userDecoration.getEndTime() <= nowTime) {
                // 过期的不管
                continue;
            }

            if (userDecoration.getDecorationId() == 106620 || userDecoration.getDecorationId() == 106633) {
                if (!map.get(106640).containsKey(userDecoration.getUserId())) {
                    map.get(106640).put(userDecoration.getUserId(), new ArrayList<>());
                }
                map.get(106640).get(userDecoration.getUserId()).add(userDecoration);
            } else if (userDecoration.getDecorationId() == 106621) {
                if (!map.get(106641).containsKey(userDecoration.getUserId())) {
                    map.get(106641).put(userDecoration.getUserId(), new ArrayList<>());
                }
                map.get(106641).get(userDecoration.getUserId()).add(userDecoration);
            } else if (userDecoration.getDecorationId() == 106622 ||
                    userDecoration.getDecorationId() == 106623 ||
                    userDecoration.getDecorationId() == 106624 ||
                    userDecoration.getDecorationId() == 106626) {
                if (!map.get(106642).containsKey(userDecoration.getUserId())) {
                    map.get(106642).put(userDecoration.getUserId(), new ArrayList<>());
                }
                map.get(106642).get(userDecoration.getUserId()).add(userDecoration);
            } else if (userDecoration.getDecorationId() == 106634 || userDecoration.getDecorationId() == 106625) {
                if (!map.get(106643).containsKey(userDecoration.getUserId())) {
                    map.get(106643).put(userDecoration.getUserId(), new ArrayList<>());
                }
                map.get(106643).get(userDecoration.getUserId()).add(userDecoration);
            } else if (userDecoration.getDecorationId() == 106627 ||
                    userDecoration.getDecorationId() == 106628 ||
                    userDecoration.getDecorationId() == 106629 ||
                    userDecoration.getDecorationId() == 106630 ||
                    userDecoration.getDecorationId() == 106631 ||
                    userDecoration.getDecorationId() == 106632 ||
                    userDecoration.getDecorationId() == 106635 ||
                    userDecoration.getDecorationId() == 106636 ||
                    userDecoration.getDecorationId() == 106637 ||
                    userDecoration.getDecorationId() == 106638) {
                if (!map.get(106644).containsKey(userDecoration.getUserId())) {
                    map.get(106644).put(userDecoration.getUserId(), new ArrayList<>());
                }
                map.get(106644).get(userDecoration.getUserId()).add(userDecoration);
            }
        }

        // 组装新称号
        List<UserDecoration> insertUserDecorations = new ArrayList<>();
        for (int newDecorationId : map.keySet()) {
            for (long userId : map.get(newDecorationId).keySet()) {
                long remainTime = 0;
                for (UserDecoration userDecoration : map.get(newDecorationId).get(userId)) {
                    remainTime += (userDecoration.getEndTime() - nowTime);
                }
                UserDecoration data = new UserDecoration();
                data.setUserId(userId);
                data.setDecorationId(newDecorationId);
                data.setEndTime(nowTime + remainTime);
                data.setModifyTime(nowTime);
                insertUserDecorations.add(data);
            }
        }

        // 插入新称号
        insertUserDecoration(insertUserDecorations);

        // 删除被合并称号
        deleteDecoration();

        //打印被删除的记录
        for (UserDecoration userDecoration : userDecorationList) {
            getLogger().error("old-serverId:{},userId:{},decorationId:{},endTime:{},modifyTime{}", getGameServer().getServerInfo().getServerId(), userDecoration.getUserId(), userDecoration.getDecorationId(), userDecoration.getEndTime(), userDecoration.getModifyTime());
        }

        //打印新称号
        for (UserDecoration userDecoration : insertUserDecorations) {
            getLogger().error("new-serverId:{},userId:{},decorationId:{},endTime:{},modifyTime{}", getGameServer().getServerInfo().getServerId(), userDecoration.getUserId(), userDecoration.getDecorationId(), userDecoration.getEndTime(), userDecoration.getModifyTime());
        }


    }

    public List<UserDecoration> getUserDecorationList() {
        List<UserDecoration> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall("select * from t_u_decoration where endTime > 0 and (decorationId between 106620 and 106638)");
            rs = call.executeQuery();
            while (rs.next()) {
                UserDecoration data = new UserDecoration();
                data.setUserId(rs.getLong("userId"));
                data.setDecorationId(rs.getInt("decorationId"));
                data.setEndTime(rs.getLong("endTime"));
                data.setModifyTime(rs.getLong("modifyTime"));
                list.add(data);
            }
        } catch (SQLException e) {
            getGameServer().getLogger().error("异常错误", e);
        } finally {
            getGameServer().closeConnection(call, rs);
        }

        return list;
    }


    private void insertUserDecoration(List<UserDecoration> userDecorationList) {
        String sql = "insert into t_u_decoration(userId,decorationId,endTime,modifyTime) values (?,?,?,?)";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        try {
            for (UserDecoration info : userDecorationList) {
                Map<Integer, DbParameter> para = new HashMap<>();
                int i = 1;
                para.put(i++, new DbParameter(Types.BIGINT, info.getUserId()));
                para.put(i++, new DbParameter(Types.INTEGER, info.getDecorationId()));
                para.put(i++, new DbParameter(Types.BIGINT, info.getEndTime()));
                para.put(i++, new DbParameter(Types.BIGINT, info.getModifyTime()));
                mapList.add(para);
            }

            getGameServer().executeBatchQuery(sql, mapList);
        } catch (Exception e) {
            getLogger().error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
    }

    public void deleteDecoration() {
        PreparedStatement call = null;
        String sql = "delete from t_u_decoration where endTime > 0 and (decorationId between 106620 and 106638)";
        try {
            call = getConnection().prepareStatement(sql);
            call.executeUpdate();
        } catch (Exception e) {
            getLogger().error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getGameServer().closeConnection(call, null);
        }
    }


}
